package com.twu.biblioteca;


import java.util.Scanner;

public class BibliotecaApp {

    private static UserInteraction ui = new UserInteraction();
    private static Library lib = new Library();
    private static User currentUser = new User();

    public static void main(String[] args) {
        System.out.println("Welcome!");
        Integer option = -1;

        while (!option.equals("0")){
            System.out.println("Please, select one of the following options.");
            System.out.println(ui.printAllValidOptions());
            option = ui.readIntegerFromKeyboard();
            if (ui.getValidOptions().containsKey(option)){
                redirect(option);
            }else{
                System.out.println("Select a valid option!!");
            }
        }
    }

    private static void redirect(Integer option){
        switch(option){
            case 0:
                break;
            case 1:
                System.out.println(login());
                break;
            case 2:
                System.out.println(lib.booksToString());
                break;
            case 3:
                System.out.println(lib.moviesToString());
                break;
            case 4:
                checkMembershipDetails();
                break;
            case 5:
                addBook();
                break;
            case 6:
                System.out.println(reserveBook());
                break;
            case 7:
                logOut();
                break;
        }
    }

    private static String reserveBook(){
        if (currentUser.isLoggedIn()){
            String title = getTitle();
            Book book = lib.findBook(title);
            if (book == null)
                return "Sorry we don't have that book yet";
            if (lib.reserveBook(book))
                return "Thank You! Enjoy the book.";

            return "Sorry, the book is already reserved.";
        }
        return "You must be logged in to perform this action";
    }

    private static void checkMembershipDetails() {
        if (!currentUser.isLoggedIn())
            System.out.println("Please talk to a Librarian. Thank you.");
        else
            System.out.println(currentUser.toString());
    }

    private static String login() {
        String username = getUsername();
        String password = getPassword();
        currentUser = lib.login(username, password);

        if (currentUser == null){
            return "Sorry, something went wrong.";
        }
        if (currentUser.isLoggedIn()){
            return "You're already logged in";
        }
        return "You're logged in!";
    }

    private static void addBook() {
        if (currentUser.isLoggedIn()){
            String title = getTitle();
            String author = getAuthor();
            Book book = new Book(author, title);
            lib.addBook(book);
        }else{
            System.out.println("You must be logged in to perform this action");
        }
    }

    private static String getAuthor() {
        System.out.println("Enter the book's author");
        return ui.readFromKeyboard();
    }

    private static String getTitle() {
        System.out.println("Enter the book's title");
        return ui.readFromKeyboard();
    }

    private static void logOut() {
        currentUser = new User();
        System.out.println("Logged Out");
    }

    private static String getPassword() {
        System.out.println("Enter the password");
        return ui.readFromKeyboard();
    }

    private static String getUsername() {
        System.out.println("Enter the username");
        return ui.readFromKeyboard();
    }

}
